<?php
/**
 * Created by PhpStorm.
 * User: blue
 * Date: 2017/11/13
 * Time: 21:17
 */

namespace server;


use PHPMailer\PHPMailer\PHPMailer;

class EmailServer
{
    public static function sendEmail($to,$title,$content){
        $mail=new PHPMailer();
        $email_from=config('msg.email_from');
        $email_from_name=config('msg.email_signature');
        $email_uname=config('msg.email_uname');
        $email_pwd=config('msg.email_pwd');
        $email_port=config('msg.email_port');
        $email_host=config('msg.email_host');
        if(empty($email_host)||empty($email_uname)||empty($email_pwd)||empty($email_from)){
            return ['status'=>'error','message'=>'邮箱格式配置不完整'];
        }
        // 设置为要发邮件
        $mail->IsSMTP();
        // 是否允许发送HTML代码做为邮件的内容
        $mail->IsHTML(TRUE);
        $mail->CharSet='UTF-8';
        // 是否需要身份验证
        $mail->SMTPAuth=TRUE;
        /*  邮件服务器上的账号是什么 -> 到163注册一个账号即可 */
        $mail->From=$email_from;
        $mail->FromName=$email_from_name;
        $mail->Host=$email_host;
        $mail->Username=$email_uname;
        $mail->Password=$email_pwd;
        $mail->Port = $email_port;
        // 收件人
        $mail->AddAddress($to);
        // 邮件标题
        $mail->Subject=$title;
        // 邮件内容
        $mail->Body=$content;
        //发送邮件,并返回状态代码
        if($mail->Send()){
            return ['status'=>'success','message'=>'邮件发送成功'];
        }else{
            $emailError=$mail->ErrorInfo;
            return ['status'=>'error','message'=>$emailError];
        }
    }
}